package com.softtown.modules.dapai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.modules.dapai.dao.CompCustomerAppDao;
import com.softtown.modules.dapai.dao.CompCustomerResourceDao;
import com.softtown.modules.dapai.dto.CustomerAddAppDTO;
import com.softtown.modules.dapai.dto.CustomerAddResourceDTO;
import com.softtown.modules.dapai.dto.CustomerAppDTO;
import com.softtown.modules.dapai.dto.CustomerResourceDTO;
import com.softtown.modules.dapai.entity.CompCustomerAppEntity;
import com.softtown.modules.dapai.entity.CompCustomerResourceEntity;
import com.softtown.modules.dapai.service.CompCustomerAppService;
import com.softtown.modules.dapai.service.CompCustomerResourceService;
import com.softtown.modules.organization.entity.CompOrgEmplEntity;
import com.softtown.modules.organization.service.CompOrgEmplService;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.utils.DateUtil;
import com.softtown.modules.utils.Tools;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CompCustomerResourceServiceImpl extends
        BaseServiceImpl<CompCustomerResourceDao, CompCustomerResourceEntity> implements CompCustomerResourceService {

    @Resource
    private CompCustomerAppService compCustomerAppService;
    @Resource
    private CompOrgEmplService compOrgEmplService;

    @Override
    @Transactional
    public void addResourceApp(CustomerAddResourceDTO customerAddResourceDTO) {
        CompOrgEmplEntity compOrgEmplEntity = compOrgEmplService.getByUserId(customerAddResourceDTO.getCompId(), SecurityUser.getUser().getId());
        CompCustomerResourceEntity compCustomerResourceEntity =ConvertUtils
                .sourceToTarget(customerAddResourceDTO, CompCustomerResourceEntity.class);
        compCustomerResourceEntity.setCustCode(compCustomerAppService.getCode());
        compCustomerResourceEntity.setEmplId(compOrgEmplEntity.getId());
        baseDao.insert(compCustomerResourceEntity);
    }

    @Override
    @Transactional
    public void updateResourceApp(CustomerAddResourceDTO customerAddResourceDTO) {
        CompCustomerResourceEntity compCustomerResourceEntity=ConvertUtils
                .sourceToTarget(customerAddResourceDTO, CompCustomerResourceEntity.class);
        baseDao.updateById(compCustomerResourceEntity);
    }

    @Override
    @Transactional
    public void deleteResourceApp(Long compId, Long id) {
        CompCustomerResourceEntity compCustomerResourceEntity =
                baseDao.selectOne(new QueryWrapper<CompCustomerResourceEntity>().lambda().eq(CompCustomerResourceEntity ::getId,id)
                        .eq(CompCustomerResourceEntity::getCompId,compId));
        compCustomerResourceEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
        baseDao.updateById(compCustomerResourceEntity);
    }

    @Override
    public PageInfo<CompCustomerResourceEntity> getResourcePageListApp(CustomerResourceDTO customerResourceDTO) {
        Integer pageSize = customerResourceDTO.getPageSize();
        Integer page = customerResourceDTO.getPage();
        if (pageSize > 0 && page > 0) {
            PageHelper.startPage(page, pageSize);
        }
        List<CompCustomerResourceEntity> compCustomerResourceEntities =
                baseDao.getResourcePageListApp(customerResourceDTO.getCompId(), customerResourceDTO.getParam());
        return  new PageInfo<>(compCustomerResourceEntities);
    }

    @Override
    public CompCustomerResourceEntity getResourceByIdApp(Long compId, Long id) {
        return baseDao.getResourceByIdApp(compId, id);
    }

    @Override
    public List<CompCustomerResourceEntity> getResourceListApp(CustomerResourceDTO customerResourceDTO) {
        QueryWrapper<CompCustomerResourceEntity> qw = new QueryWrapper<>();
        if (customerResourceDTO.getCompId() != null) {
            qw.lambda().eq(CompCustomerResourceEntity::getCompId, customerResourceDTO.getCompId());
        }
        qw.lambda().eq(CompCustomerResourceEntity::getStatus, 0);
        if (customerResourceDTO.getParam() != null) {
            qw.lambda().like(CompCustomerResourceEntity::getCustCode, customerResourceDTO.getParam()).or()
                    .like(CompCustomerResourceEntity::getName, customerResourceDTO.getParam());
        }
        return baseDao.selectList(qw);
    }
}
